package com.aijk.web.admin.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by iscdw on 2016/4/20.
 */
@Controller
@RequestMapping("/perm")
public class AdminPermissionController extends BaseController {

/*    @Autowired
    private AdminPermissionFacade adminPermissionFacade;

    @RequestMapping("/list")
    @RequiresPermissions("perm:list")
    public String list(Model model) {
        List<AdminPermission> list = adminPermissionFacade.getAll();

        model.addAttribute("list", list);
        return "/perm/list";
    }

    @ResponseBody
    @RequestMapping("/getPermList")
    @RequiresPermissions("perm:list")
    public String getList(AdminPermission permission) {
        PagingRequest page = getPage();
        PageInfo<AdminPermission> pageInfo = adminPermissionFacade.getPage(page, permission);
        return jsonPage(pageInfo);
    }

    @RequestMapping("/edit")
    @RequiresPermissions("perm:edit")
    public String edit(Model model, @RequestParam(required = false) Integer id, @RequestParam(required = false) Integer pid) {
        AdminPermissionVo vo = new AdminPermissionVo();
        if (id != null) {
            AdminPermission permission = adminPermissionFacade.get(id);
            if (permission.getPid().intValue() == 0) {
                vo.setPname("顶级菜单");
            }else{
                AdminPermission parent = adminPermissionFacade.get(permission.getPid());//获取父节点用于回显title
                vo.setPname(parent.getTitle());
            }
            BeanUtils.copyProperties(permission, vo);

        } else if (pid != null) {
            AdminPermission permission = adminPermissionFacade.get(pid);
            vo.setPid(pid);
            vo.setPname(permission.getTitle());
        }

        model.addAttribute("perm", vo);
        return "/perm/edit";
    }

    @ResponseBody
    @RequestMapping("/save")
    @RequiresPermissions("perm:edit")
    public String save(Model model, AdminPermission permission) {
        JSONObject json = new JSONObject();
        try {
            if (permission.getId() != null) {
                permission.setStatus(RecordStatus.STATUS_USE.shortValue());
                adminPermissionFacade.update(permission);
            } else {
                if (StringUtils.isEmpty(permission.getIcon())) {
                    permission.setIcon(AdminPermissionConst.DEFAULT_ICON);
                }
                adminPermissionFacade.insert(permission);
            }

            json.put("code", "0");
            json.put("msg", "保存成功");
        } catch (Exception e) {
            json.put("code", "1");
            json.put("msg", "保存失败");
        }
        return json.toJSONString();
    }

    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("perm:edit")
    public String delete(Integer id) {
        //adminPermissionFacade.delete(id);
        JSONObject json = new JSONObject();
        json.put("code", "0");
        json.put("msg", "删除成功");
        return json.toJSONString();
    }

    @RequestMapping("/tree")
    public String tree(@RequestParam(required = false) Integer selId, Model model) {

        model.addAttribute("selId", selId);
        return "/perm/tree";
    }

    @RequestMapping("/treeJson")
    @ResponseBody
    public String treeJson(@RequestParam(required = false) Integer selId) {
        JSONArray jsonArray = new JSONArray();
        List<AdminPermission> list = adminPermissionFacade.tree(0);

        JSONObject root = new JSONObject();
        root.put("id", "0");
        root.put("text", "顶级菜单");
        root.put("parent", "#");

        JSONObject state = new JSONObject();
        state.put("opened", true);
        root.put("state", state);
        jsonArray.add(root);

        for (AdminPermission permission : list) {
            JSONObject pNode = new JSONObject();
            if (selId == permission.getId()) {
                JSONObject pNodeState = new JSONObject();
                pNodeState.put("selected", true);
                pNode.put("state", pNodeState);
            }
            pNode.put("id", permission.getId());
            pNode.put("text", permission.getTitle());
            pNode.put("parent", permission.getPid());
            pNode.put("icon", "fa fa-eye");
            jsonArray.add(pNode);


        }
        return jsonArray.toJSONString();
    }

    @RequestMapping("/permTreeJson")
    @ResponseBody
    public String permTreeJson(@RequestParam(required = false) String selId) {
        JSONArray jsonArray = new JSONArray();
        List<AdminPermission> list = adminPermissionFacade.getAll();
        //List<AdminPermission> list = adminPermissionFacade.tree(0);
        JSONObject root = new JSONObject();
        root.put("id", "0");
        root.put("text", "顶级菜单");
        root.put("parent", "#");

        JSONObject state = new JSONObject();
        state.put("opened", true);
        root.put("state", state);
        jsonArray.add(root);

        String[] ids = StringUtils.split(selId, ",");
        List<String> idList = Arrays.asList(ids);

        for (AdminPermission permission : list) {
            JSONObject pNode = new JSONObject();
            if (idList.contains(String.valueOf(permission.getId()))) {
                JSONObject pNodeState = new JSONObject();
                pNodeState.put("selected", true);
                pNode.put("state", pNodeState);
            }
            pNode.put("id", permission.getId() + "");
            pNode.put("text", permission.getTitle());
            pNode.put("parent", permission.getPid() + "");
            pNode.put("icon", permission.getIcon());

            jsonArray.add(pNode);


        }
        return jsonArray.toJSONString();
    }*/
}
